/*
 * @Author: ziqi jhzq12345678
 * @Date: 2024-10-23 17:11:21
 * @LastEditors: ziqi jhzq12345678
 * @LastEditTime: 2025-01-17 19:03:35
 * @FilePath: /pornhub_app/lib/src/views/search/result/search_short_result.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'package:pornhub_app/components/keep_alive_wrapper.dart';
import 'package:pornhub_app/components/no_more/no_data.dart';
import 'package:pornhub_app/components/no_more/no_more.dart';
import 'package:pornhub_app/http/http_service.dart';
import 'package:pornhub_app/model/search/search_res_model.dart';
import 'package:pornhub_app/model/video_base_model.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/search/search_controller.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:pornhub_app/views/douyin/short_video_list_cell.dart';

class SearchShortResult extends StatefulWidget {
  final int searchType;
  const SearchShortResult({super.key, required this.searchType});

  @override
  State<SearchShortResult> createState() => _SearchShortResult();
}

class _SearchShortResult extends State<SearchShortResult> {
  final _pageSize = 40;
  final PagingController<int, VideoBaseModel> _pagingController =
      PagingController(firstPageKey: 1);
  final searchvc = Get.find<SearchPageController>();
  Future _initListData(int pageKey) async {
    final videos = await httpInstance.get<SearchResModel>(
        url: 'search/keyWord',
        queryMap: {
          'pageSize': _pageSize,
          'searchType': widget.searchType,
          'searchWord': searchvc.keyWord,
          'page': pageKey,
        },
        complete: SearchResModel.fromJson);
    final isLastPage = videos.videoList.length < _pageSize;
    if (isLastPage) {
      _pagingController.appendLastPage(videos.videoList);
    } else {
      final nextPageKey = pageKey + 1;
      _pagingController.appendPage(videos.videoList, nextPageKey as int?);
    }
  }

  @override
  void initState() {
    _pagingController.addPageRequestListener((pageKey) {
      _initListData(pageKey);
    });
    ever(searchvc.keyWord, (v) {
      _pagingController.refresh();
    });
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return KeepAliveWrapper(
      child: RefreshIndicator(
          onRefresh: () => Future.sync(
                () => _pagingController.refresh(),
              ),
          child: PagedMasonryGridView.count(
              padding: EdgeInsets.symmetric(horizontal: 14.w),
              mainAxisSpacing: 10.w,
              crossAxisSpacing: 8.w,
              pagingController: _pagingController,
              physics: const AlwaysScrollableScrollPhysics(),
              showNoMoreItemsIndicatorAsGridChild: false,
              builderDelegate: PagedChildBuilderDelegate<VideoBaseModel>(
                  noMoreItemsIndicatorBuilder: (_) {
                return const NoMore();
              }, noItemsFoundIndicatorBuilder: (_) {
                return const NoData();
              }, itemBuilder: (context, value, index) {
                final h = 238.w;
                return ShortVideoListCell(
                  width: 182.w,
                  imageHeight: h,
                  model: value,
                  keyWord: searchvc.keyWord.value,
                ).onTap(
                  () {
                    Get.toShortVideoPlay(_pagingController.itemList!,
                        idx: index);
                  },
                );
              }),
              crossAxisCount: 2)),
    );
  }
}
